package ro.gateway.aida.obj.db;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;

import ro.gateway.aida.admin.PropertyListDB;
import ro.gateway.aida.admin.PropertyListItem;
import ro.gateway.aida.db.DBPersistenceManager;
import ro.gateway.aida.db.PersistenceToken;
import ro.gateway.aida.fnd.FndPersMan;
import ro.gateway.aida.fnd.FundingItem;
import ro.gateway.aida.obj.AIDALocation;
import ro.gateway.aida.obj.AIDAObjSector;
import ro.gateway.aida.obj.Activity;
import ro.gateway.aida.obj.ActivityRelation;
import ro.gateway.aida.obj.ActivityStatus;
import ro.gateway.aida.obj.Category;
import ro.gateway.aida.obj.searchrows.ActivityRow;
import ro.gateway.aida.obj.searchrows.ActivitySelectorRow;
import ro.gateway.aida.srv.ICFService;
import ro.gateway.aida.utils.SortedList;


/**
 * <p>Title: Romanian AIDA</p> <p>Description: :D application</p> <p>Copyright: Copyright (comparator) 2003</p>
 * <p>Company: Romania Development Gateway </p>
 *
 * @author Mihai Popoaei, mihai_popoaei@yahoo.com, smike@intellisource.ro
 * @version 1.0-* @version $Id: ActivityDB.java,v 1.3 2005/03/07 17:31:21 mihaipostelnicu Exp $
 *          <p/>
 *          nota: in db, harm_status e de fapt local_id status
 */
public class ActivityDB extends DBPersistenceManager {
	private ActivityDB(PersistenceToken token) {
		super(token);
	}

	public static ActivityDB getManager(PersistenceToken token) {
		return new ActivityDB(token);
	}

	public long getLastId() throws SQLException {
		long result = 0;
		Connection con = getConnection();

		try {
			PreparedStatement ps = con.prepareStatement(GET_MAX_ID);
			ResultSet rs = ps.executeQuery();

			if (rs.next())
				result = rs.getLong(1);

			rs.close();
			ps.close();
		} catch (SQLException sqlEx) {
			throw sqlEx;
		} finally {
			con.close();
		}

		return result;
	}

	public Activity getById(long id) throws SQLException {
		Connection con = getConnection();
		Activity result = null;

		try {
			PreparedStatement ps = con.prepareStatement(GET_BY_ID);
			ps.setLong(1, id);

			ResultSet rs = ps.executeQuery();

			if (rs.next()) {
				result = new Activity();

				result.setId(id);
				result.setParentId(rs.getLong(1));

				if (rs.wasNull())
					result.setParentId(-1);

				Date zadate = rs.getDate(2);
				result.setDate_pa((zadate != null) ? zadate.getTime() : (-1));

				if (rs.wasNull())
					result.setDate_pa(-1);

				zadate = rs.getDate(3);
				result.setDate_aa((zadate != null) ? zadate.getTime() : (-1));

				if (rs.wasNull())
					result.setDate_aa(-1);

				zadate = rs.getDate(4);
				result.setDate_ps((zadate != null) ? zadate.getTime() : (-1));

				if (rs.wasNull())
					result.setDate_ps(-1);

				zadate = rs.getDate(5);
				result.setDate_as((zadate != null) ? zadate.getTime() : (-1));

				if (rs.wasNull())
					result.setDate_as(-1);

				zadate = rs.getDate(6);
				result.setDate_pe((zadate != null) ? zadate.getTime() : (-1));

				if (rs.wasNull())
					result.setDate_pe(-1);

				zadate = rs.getDate(7);
				result.setDate_ae((zadate != null) ? zadate.getTime() : (-1));

				if (rs.wasNull())
					result.setDate_ae(-1);

				result.setInternational(rs.getInt(8) == 1);
				result.setTransfrontalier(rs.getInt(9) == 1);
				zadate = rs.getDate(10);
				result.setDate_ext((zadate != null) ? zadate.getTime() : (-1));

				if (rs.wasNull())
					result.setDate_ext(-1);

				long status = rs.getLong(11);

				if (rs.wasNull()) {
					status = -1;
					result.setStatus(null);
				} else
					result.setStatus(ActivityStatusDB
						  .getManager(db_token).getById(status));

				String logo_url = rs.getString(12);
				if (rs.wasNull())
					result.setLogo_url(null);
				else
					result.setLogo_url(logo_url);

				result.setApproved(DBPersistenceManager.str2Bool(rs.getString(13)));
				result.setInfoSource(rs.getString(14));
				if (rs.wasNull()) {
					result.setInfoSource(null);
				}

				result.setStatus_automation_on(rs.getInt(15) != 0);
			}

			rs.close();

			ps.close();
		} catch (SQLException sqlEx) {
			throw sqlEx;
		} finally {
			con.close();
		}

		return result;
	}

	public ArrayList getByQuery(long id, String Query)
		  throws SQLException {
		Connection con = getConnection();
		ArrayList result = new ArrayList();
		Activity c = null;

		try {
			PreparedStatement ps = con.prepareStatement(GET_BY_QUERY + Query +
				  ")");
			ps.setLong(1, id);

			ResultSet rs = ps.executeQuery();

			while (rs.next()) {
				c = new Activity();

				c.setId(id);
				c.setParentId(rs.getLong(1));

				if (rs.wasNull())
					c.setParentId(-1);

				Date zadate = rs.getDate(2);
				c.setDate_pa((zadate != null) ? zadate.getTime() : (-1));

				if (rs.wasNull())
					c.setDate_pa(-1);

				zadate = rs.getDate(3);
				c.setDate_aa((zadate != null) ? zadate.getTime() : (-1));

				if (rs.wasNull())
					c.setDate_aa(-1);

				zadate = rs.getDate(4);
				c.setDate_ps((zadate != null) ? zadate.getTime() : (-1));

				if (rs.wasNull())
					c.setDate_ps(-1);

				zadate = rs.getDate(5);
				c.setDate_as((zadate != null) ? zadate.getTime() : (-1));

				if (rs.wasNull())
					c.setDate_as(-1);

				zadate = rs.getDate(6);
				c.setDate_pe((zadate != null) ? zadate.getTime() : (-1));

				if (rs.wasNull())
					c.setDate_pe(-1);

				zadate = rs.getDate(7);
				c.setDate_ae((zadate != null) ? zadate.getTime() : (-1));

				if (rs.wasNull())
					c.setDate_ae(-1);

				c.setInternational(rs.getInt(8) == 1);
				c.setTransfrontalier(rs.getInt(9) == 1);
				zadate = rs.getDate(10);
				c.setDate_ext((zadate != null) ? zadate.getTime() : (-1));

				if (rs.wasNull())
					c.setDate_ext(-1);

				long status = rs.getLong(11);

				if (rs.wasNull()) {
					status = -1;
					c.setStatus(null);
				} else
					c.setStatus(ActivityStatusDB.getManager(db_token).getById(status));

				String logo_url = rs.getString(12);

				if (rs.wasNull())
					c.setLogo_url(null);
				else
					c.setLogo_url(logo_url);

				c.setApproved(DBPersistenceManager.str2Bool(rs.getString(13)));
				c.setInfoSource(rs.getString(14));
				if (rs.wasNull()) c.setInfoSource(null);

				c.setStatus_automation_on(rs.getInt(15) != 0);
			}

			rs.close();

			ps.close();
		} catch (SQLException sqlEx) {
			throw sqlEx;
		} finally {
			con.close();
		}

		return result;
	}

	public long insert(Activity activity) throws SQLException {
		long ret = 0;
		Connection con = getConnection();

		if (activity.getId() == -1) {
			AIDAActivityObjectDB.getManager(token).insert(db_token.getIdgenerators(),
				  activity.getObject());
			activity.setId(activity.getObject().getId());
		}

		ret = activity.getId();

		try {
			PreparedStatement ps = con.prepareStatement(INSERT);

			ps.setLong(1, activity.getId());

			if (activity.getParentId() != -1)
				ps.setLong(2, activity.getParentId());
			else
				ps.setNull(2, Types.INTEGER);

			if (activity.getDate_pa() != -1)
				ps.setDate(3, new Date(activity.getDate_pa()));
			else
				ps.setNull(3, Types.DATE);

			if (activity.getDate_aa() != -1)
				ps.setDate(4, new Date(activity.getDate_aa()));
			else
				ps.setNull(4, Types.DATE);

			if (activity.getDate_ps() != -1)
				ps.setDate(5, new Date(activity.getDate_ps()));
			else
				ps.setNull(5, Types.DATE);

			if (activity.getDate_as() != -1)
				ps.setDate(6, new Date(activity.getDate_as()));
			else
				ps.setNull(6, Types.DATE);

			if (activity.getDate_pe() != -1)
				ps.setDate(7, new Date(activity.getDate_pe()));
			else
				ps.setNull(7, Types.DATE);

			if (activity.getDate_ae() != -1)
				ps.setDate(8, new Date(activity.getDate_ae()));
			else
				ps.setNull(8, Types.DATE);

			ps.setInt(9, activity.isInternational() ? 1 : 0);
			ps.setInt(10, activity.isTransfrontalier() ? 1 : 0);

			if (activity.getDate_ext() != -1)
				ps.setDate(11, new Date(activity.getDate_ext()));
			else
				ps.setNull(11, Types.DATE);

			ActivityStatus status = activity.getStatus();

			if (status != null) {
				if (status.getId() == -1)
					ActivityStatusDB.getManager(db_token)
						  .insert(status);

				ps.setLong(12, status.getId());
			} else
				ps.setNull(12, Types.INTEGER);

			if (activity.getLogo_url() != null)
				ps.setString(13, activity.getLogo_url());
			else
				ps.setNull(13, Types.VARCHAR);

			if (activity.getInfoSource() != null) {
				ps.setString(14, activity.getInfoSource());
			} else {
				ps.setNull(14, Types.VARCHAR);
			}

			ps.setInt(15, activity.isStatus_automation_on() ? 1 : 0);

			ps.executeUpdate();

			ps.close();
		} catch (SQLException sqlEx) {
			throw sqlEx;
		} finally {
			con.close();
		}

		return ret;
	}

	public void update(Activity activity) throws SQLException {
		Connection con = getConnection();

		try {
			PreparedStatement ps = con.prepareStatement(UPDATE);

			if (activity.getParentId() == -1)
				ps.setNull(1, Types.INTEGER);
			else
				ps.setLong(1, activity.getParentId());

			if (activity.getDate_pa() != -1)
				ps.setDate(2, new Date(activity.getDate_pa()));
			else
				ps.setNull(2, Types.DATE);

			if (activity.getDate_aa() != -1)
				ps.setDate(3, new Date(activity.getDate_aa()));
			else
				ps.setNull(3, Types.DATE);

			if (activity.getDate_ps() != -1)
				ps.setDate(4, new Date(activity.getDate_ps()));
			else
				ps.setNull(4, Types.DATE);

			if (activity.getDate_as() != -1)
				ps.setDate(5, new Date(activity.getDate_as()));
			else
				ps.setNull(5, Types.DATE);

			if (activity.getDate_pe() != -1)
				ps.setDate(6, new Date(activity.getDate_pe()));
			else
				ps.setNull(6, Types.DATE);

			if (activity.getDate_ae() != -1)
				ps.setDate(7, new Date(activity.getDate_ae()));
			else
				ps.setNull(7, Types.DATE);

			ps.setInt(8, activity.isInternational() ? 1 : 0);
			ps.setInt(9, activity.isTransfrontalier() ? 1 : 0);

			if (activity.getDate_ext() != -1)
				ps.setDate(10, new Date(activity.getDate_ext()));
			else
				ps.setNull(10, Types.DATE);

			ActivityStatus status = activity.getStatus();

			if (status != null) {
				if (status.getId() == -1)
					ActivityStatusDB.getManager(db_token)
						  .insert(status);

				ps.setLong(11, status.getId());
			} else
				ps.setNull(11, Types.INTEGER);

			if (activity.getLogo_url() != null)
				ps.setString(12, activity.getLogo_url());
			else
				ps.setNull(12, Types.VARCHAR);

			if (activity.getInfoSource() != null) {
				ps.setString(13, activity.getInfoSource());
			} else {
				ps.setNull(13, Types.VARCHAR);
			}

			ps.setInt(14, activity.isStatus_automation_on() ? 1 : 0);

			ps.setLong(15, activity.getId());
			ps.executeUpdate();

			ps.close();
		} catch (SQLException sqlEx) {
			throw sqlEx;
		} finally {
			con.close();
		}
	}

	/**
	 * deletes an activity. This does NOT delete al the linked data, but just the activity row
	 *
	 * @param id the id of the activitiy to be deleted
	 * @throws SQLException
	 */
	public void delete(long id) throws SQLException {
		Connection con = getConnection();

		try {
			PreparedStatement ps = con.prepareStatement(DELETE);
			ps.setLong(1, id);
			ps.executeUpdate();
			ps.close();
		} catch (SQLException sqlEx) {
			throw sqlEx;
		} finally {
			con.close();
		}
	}

	/**
	 * creates a complete Activity object, fetching all required data and creating the related beans
	 * (sectors,locations,etc...)
	 *
	 * @param id activity_id
	 * @return the complete activity object
	 * @throws SQLException
	 */
	public Activity getAllDataById(long id) throws SQLException {
		ICFService cfService = db_token.getCFService();
		String poolName = db_token.getPool_name();

		Activity bean = getById(id);

		if (bean == null)
			return null;

		bean.setObject(AIDAActivityObjectDB.getManager(token).getById(id));

		// generic properties
		bean.setGen_titles(GenericActivityPropertyDB.getManager(token)
			  .getFirstForActivity("gen_titles",
				    id));
		bean.setGen_status_reasons(GenericActivityPropertyDB.getManager(token)
			  .getFirstForActivity("gen_status",
				    id));
		bean.setGen_descriptions(GenericActivityPropertyDB.getManager(token)
			  .getFirstForActivity("gen_descriptions",
				    id));
		bean.setGen_notefield1(GenericActivityPropertyDB.getManager(token)
			  .getFirstForActivity("gen_notefields1",
				    id));
		bean.setGen_notefield2(GenericActivityPropertyDB.getManager(token)
			  .getFirstForActivity("gen_notefields2",
				    id));
		bean.setGen_notefield3(GenericActivityPropertyDB.getManager(token)
			  .getFirstForActivity("gen_notefields3",
				    id));
		bean.setGen_notefield4(GenericActivityPropertyDB.getManager(token)
			  .getFirstForActivity("gen_notefields4",
				    id));
		bean.setGen_ext_reasons(GenericActivityPropertyDB.getManager(token)
			  .getFirstForActivity("gen_ext_reasons",
				    id));
		bean.setGen_keywords(GenericActivityPropertyDB.getManager(token)
			  .getFirstForActivity("gen_keywords",
				    id));
		bean.setGen_bencomment(GenericActivityPropertyDB.getManager(token)
			  .getFirstForActivity("gen_bencomments", id));
		bean.setGen_objectives(GenericActivityPropertyDB.getManager(token)
			  .getFirstForActivity("gen_objectives",
				    id));
		bean.setGen_results(GenericActivityPropertyDB.getManager(token)
			  .getFirstForActivity("gen_results",
				    id));
		bean.addComponents(GenericActivityPropertyDB.getManager(token)
			  .getAllForActivity("gen_components",
				    id));

		// organizations, persons
		OrganizationInvDB oinv = OrganizationInvDB.getManager(token);
		bean.addOrgs(oinv.getForActivity(id), null);
		bean.addPersons(PersonInvDB.getManager(token).getForActivity(id));

		bean.addDocument(AIDADocumentDB.getManager(token).getForActivity(id));
		bean.addResources(AIDAResourceDB.getManager(token).getForActivity(id));

		DBStuff.getManager(db_token).setDonorIds(bean);

		AIDALocation[] locations = AIDALocationDB.getManager(token)
			  .getForActivity(id);

		if (locations == null)
			locations = new AIDALocation[0];

		for (int i = 0; i < locations.length; i++)
			bean.addLocation(locations[i]);

		AIDAObjSector[] sectors = AIDAObjSectorDB.getManager(db_token)
			  .getForActivity(id);

		if (sectors == null)
			sectors = new AIDAObjSector[0];

		for (int i = 0; i < sectors.length; i++)
			bean.addSector(sectors[i]);

		FundingItem[] funding_items = FndPersMan.getManager(db_token)
			  .getFundingsByActivity(id);

		if (funding_items == null)
			funding_items = new FundingItem[0];

		for (int i = 0; i < funding_items.length; i++) {
			bean.addFndItem(funding_items[i]);
//               System.out.println( "found funding item: " +
//                  funding_items[ i ].getId() );

			//                  bean.getFunding_items().add( funding_items[ i ] );
		}

		ActivityRelation[] relations = ActivityRelationDB.getManager(token)
			  .getForActivity(id);

		if (relations == null)
			relations = new ActivityRelation[0];

		for (int i = 0; i < relations.length; i++)
			bean.getAct_relations().add(relations[i]);

		Category[] categories = DBStuff.getManager(db_token).get_cats_for_activity(id);

		if (categories == null)
			categories = new Category[0];

		for (int i = 0; i < categories.length; i++) {
			Category category = categories[i];
			bean.addCategory(category);
		}

		bean.setContact(ContactInfoPMan.getManager(db_token).getForActivity(id));
		getBeneficiaries(bean);

		return bean;
	}

	/**
	 * returns an arraylist conaining activity rows (search rows) with latest 'count' updated project, order by update
	 * date. The rows are unilingual
	 *
	 * @param lang  the lang of the returned content
	 * @param count the number of rows to be returned
	 * @return an arraylist conaining activity rows (search rows) with latest 'count'
	 * @throws SQLException
	 */
	public ArrayList getWhatsNew(String lang, int count)
		  throws SQLException {
		ArrayList res = new ArrayList();
		Connection con = getConnection();

		try {
			int c = 0;
			PreparedStatement ps = con.prepareStatement(GET_UPDATED_ACT);
			ps.setString(1, lang);

			ResultSet rs = ps.executeQuery();

			while (rs.next()) {
				if (++c > count)
					break;

				ActivityRow ar = new ActivityRow();
				ar.setTitle(rs.getString(1));
				ar.setDate(rs.getDate(3));
				ar.setId(rs.getLong(2));
				res.add(ar);
			}
		} finally {
			con.close();
		}

		return res;
	}

	
	
	public ArrayList getAllDataLatest(int count)
		  throws SQLException {
		ArrayList res = new ArrayList();
		Connection con = getConnection();

		try {
			int c = 0;
			PreparedStatement ps = con.prepareStatement(GET_UPDATED_IDS);
			ps.setInt(1, count);

			ResultSet rs = ps.executeQuery();

			while (rs.next()) {
			    long id=rs.getLong(1);
			    Activity a=getAllDataById(id);
			    res.add(a);
			}
		} finally {
			con.close();
		}

		return res;
	}

	
	
	/**
	 * returns the owner of an activity, -1 if the activity could not be found
	 *
	 * @param activity_id the activity id
	 * @return the owner of the activity object
	 * @throws SQLException
	 */
	public long getOwner(long activity_id) throws SQLException {
		long result = -1;
		Connection con = getConnection();

		try {
			PreparedStatement ps = con.prepareStatement(GET_OWNER);
			ps.setLong(1, activity_id);

			ResultSet rs = ps.executeQuery();

			if (rs.next())
				result = rs.getLong(1);

			rs.close();
			ps.close();
		} catch (SQLException sqlEx) {
			throw sqlEx;
		} finally {
			con.close();
		}

		return result;
	}

	/**
	 * @param praja_id
	 * @return
	 * @throws SQLException
	 * @deprecated
	 */
	public long getActivityIdByPrajaId(String praja_id)
		  throws SQLException {
		long result = -1;
		Connection con = getConnection();

		try {
			PreparedStatement ps = con.prepareStatement(GET_ACT_ID_BY_PRAJA);
			ps.setString(1, praja_id);

			ResultSet rs = ps.executeQuery();

			if (rs.next())
				result = rs.getLong(1);

			rs.close();
			ps.close();
		} catch (SQLException sqlEx) {
			throw sqlEx;
		} finally {
			con.close();
		}

		return result;
	}


	public SortedList selectorSearch(String title, String lang)
		  throws SQLException {
		Connection con = getConnection();
		SortedList sl = new SortedList();
		title = title.trim();
		try {
			PreparedStatement ps = con.prepareStatement(SELECTOR_SEARCH);
			ps.setString(1, lang);
			ps.setString(2, "%" + title + "%");

			ResultSet rs = ps.executeQuery();

			while (rs.next()) {
				ActivitySelectorRow psr = new ActivitySelectorRow();
				psr.setActivityId(rs.getLong(1));
				psr.setName(rs.getString(2));
				sl.add(psr);
			}
		} finally {
			con.close();
		}

		return sl;
	}

	public void getBeneficiaries(Activity activity) throws SQLException {
		Connection con = getConnection();

		PropertyListDB propdb = PropertyListDB.getManager(db_token);
		try {
			PreparedStatement ps = con.prepareStatement(GET_BENEFICIARIES_FOR_ACTIVITY);
			ps.setLong(1, activity.getId());
			ResultSet rs = ps.executeQuery();
			while (rs.next()) {
				long prop_id = rs.getLong(1);
				activity.getBeneficiaries().add(propdb.getItemById(prop_id));
			}
			rs.close();
			ps.close();
		} catch (SQLException sqlex) {
			throw sqlex;
		} finally {
			con.close();
		}
	}

	public void deleteBeneficiaries(Activity activity) throws SQLException {
		Connection con = getConnection();

		try {
			PreparedStatement ps = con.prepareStatement(DELETE_BENEFICIARIES_FOR_ACTIVITY);
			ps.setLong(1, activity.getId());
			ps.executeUpdate();
			ps.close();
		} catch (SQLException sqlEx) {
			throw sqlEx;
		} finally {
			con.close();
		}
	}

	public void setBeneficiaries(Activity activity) throws SQLException {
		ArrayList benz = activity.getBeneficiaries();
		if (benz == null || benz.size() < 1) return;

		Connection con = getConnection();

		try {
			PreparedStatement ps = con.prepareStatement(SET_BENZ_FOR_ACTIVITY);
//			private static final String SET_BENZ_FOR_ACTIVITY =
//				 "INSERT INTO activities_benz (activity_id, prop_id) VALUES (?,?)";
			for (int i = 0; i < benz.size(); i++) {
				PropertyListItem item = (PropertyListItem) benz.get(i);
				ps.clearParameters();
				ps.setLong(1, activity.getId());
				ps.setLong(2, item.getId());
				ps.executeUpdate();
			}
			ps.close();
		} catch (SQLException sqlEx) {
			throw sqlEx;
		} finally {
			con.close();
		}
	}

	public boolean activityExists(long id) {
		if (id < 1) return false;
		boolean exists = false;
		Connection con = getConnection();
		try {
			PreparedStatement ps = con.prepareStatement(ACTIVITY_EXISTS);
			ps.setLong(1, id);
			ResultSet rs = ps.executeQuery();
			if (rs.next()) {
				exists = rs.getInt(1) > 0;
			}
			rs.close();
			ps.close();
		} catch (SQLException sqlex) {
			sqlex.printStackTrace();
		} finally {
			try {
				con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}

		return exists;
	}

	private static final String SELECTOR_SEARCH = "SELECT DISTINCT a.id,lpd_get_prj_title_name(t.id,?) FROM activities a, gen_titles t,objects o WHERE " +
		  "a.id=o.activity_id AND o.obj_type=12 AND o.id=t.id AND UPPER(t.name) ILIKE(?)";

	private static final String GET_UPDATED_IDS="SELECT DISTINCT activity_id,date_modif FROM objects ORDER BY date_modif DESC,activity_id LIMIT ?";
	
	private static final String GET_UPDATED_ACT =
		  "SELECT DISTINCT lpd_get_prj_title_name(o.id,?),o.activity_id,o.date_modif FROM " +
		  " objects o, activities a WHERE " +
		  " a.id=o.activity_id AND a.approved='Y' AND o.obj_type=12 ORDER by o.date_modif DESC";
	private static final String GET_MAX_ID = "SELECT max(id) FROM activities";
	private static final String INSERT =
		  "INSERT INTO activities (id, parent_id, date_pa, date_aa, date_ps," +
		  "date_as, date_pe, date_ae, is_in, is_tf, date_ext, harm_status, logo_url, infosource, autostatuson) " +
		  "VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
	private static final String UPDATE =
		  "UPDATE activities SET parent_id=?, date_pa=?, date_aa=?, date_ps=?," +
		  "date_as=?, date_pe=?, date_ae=?, is_id=?, is_tf=?, date_ext=?, harm_status=?, logo_url=?, infosource=?, autostatuson=? " +
		  "WHERE id = ?";
	private static final String GET_BY_ID =
		  "SELECT parent_id, date_pa, date_aa, date_ps, date_as, date_pe, " +
		  "date_ae, is_in, is_tf, date_ext, harm_status, logo_url, approved, infosource, autostatuson " +
		  "FROM activities WHERE id = ?";
	private static final String GET_BY_QUERY =
		  "SELECT parent_id, date_pa, date_aa, date_ps, date_as, date_pe, " +
		  "date_ae, is_in, is_tf, date_ext, harm_status, logo_url, approved, infosource, autostatuson " +
		  "FROM activities WHERE id in (";
	private static final String DELETE = "DELETE FROM activities " +
		  "WHERE id = ?";
	private static final String GET_ACT_ID_BY_PRAJA = "SELECT id FROM activities WHERE praja_id=?";
	private static final String GET_OWNER = "SELECT user_id FROM objects WHERE activity_id=? " +
		  "AND obj_type=0";
	private static final String GET_BENEFICIARIES_FOR_ACTIVITY =
		  "SELECT prop_id FROM activities_benz WHERE activity_id=?";
	private static final String DELETE_BENEFICIARIES_FOR_ACTIVITY =
		  "DELETE FROM activities_benz WHERE activity_id=?";
	private static final String SET_BENZ_FOR_ACTIVITY =
		  "INSERT INTO activities_benz (activity_id, prop_id) VALUES (?,?)";
	private static final String ACTIVITY_EXISTS = "SELECT count(1) FROM activities WHERE id=?";
}
